import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
// import { IpcRenderer } from 'electron'
// import { runStart } from 'comzen-deploy'
// runStart('casehub-vue-ui')

// IpcRenderer.send('ping')
// Custom APIs for renderer
ipcRenderer.on('msg', (event, mess) => {
  console.log(mess, '11111111111111-on')
})
const api = {
  getConfigDate: async (callback) => {
    const res = await ipcRenderer.invoke('getConfigDate')
    callback(res)
  },
  ping2: (val) => ipcRenderer.invoke('ping2', val),
  saveJson: async (val, callback) => {
    const res = await ipcRenderer.invoke('saveJson', val)
    callback(res)
  },
  progressNotice: (callback: (val) => void) => {
    ipcRenderer.on('progressNotice', (_event, val) => {
      callback(val)
    })
  }
}
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
// contextBridge.exposeInMainWorld('api', {
//   ping: () => ipcRenderer.invoke('ping')
// })
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
